package suncertify.view.client;

import suncertify.util.Messages;
import suncertify.util.Options;
import suncertify.view.SwingUtils;
import suncertify.view.SCButton;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Client options dialog
 */
public class OptionsDialog extends JDialog {
    private static final Dimension minSize = new Dimension(320, 120);
    private static final Messages msg = Messages.getInstance();
    private static final Options opts = Options.getInstance();

    /**
     * constructor
     * @param parent parent frame
     */
    public OptionsDialog(JFrame parent) {
        super(parent, msg.get("ui.client.options.title"), true);
        initComponents();
    }

    private void initComponents() {
        setMinimumSize(minSize);
        JButton saveBtn = new SCButton(msg.get("ui.client.options.button.save"));
        JButton cancelBtn = new SCButton(msg.get("ui.client.options.button.cancel"));



        final JTextField localDbPath = new JTextField(opts.get(Options.LOCAL_DB_FILE));
        final JTextField remoteServerAddr = new JTextField(opts.get(Options.SERVER_IP));

        saveBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                opts.set(Options.LOCAL_DB_FILE, localDbPath.getText());
                opts.set(Options.SERVER_IP, remoteServerAddr.getText());
                opts.save();
                setVisible(false);
            }
        });

        cancelBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        JPanel panel = new JPanel(gbl);

        gbc.insets = new Insets(5, 10, 5, 10);
        gbc.weightx = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        panel.add(new JLabel(msg.get("ui.client.options.label.server")), gbc);
        gbc.weightx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        panel.add(remoteServerAddr, gbc);
        gbc.weightx = 0;
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        panel.add(new JLabel(msg.get("ui.client.options.label.db.path")), gbc);
        gbc.weightx = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        panel.add(localDbPath, gbc);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(saveBtn);
        buttonPanel.add(cancelBtn);
        panel.add(buttonPanel, gbc);

        setContentPane(panel);
        pack();
    }

    /**
     * shows or hides this component depending on the value of parameter
     * @param val if true, shows this component; otherwise, hides this component
     */
    @Override
    public void setVisible(boolean val) {
        if (val) {
            SwingUtils.placeCenter(getParent(), this);
        }
        super.setVisible(val);
    }
}
